﻿@model KL.Store.Web.Models.StockXmlImportModel
@using KL.Store.Core
@{
    Layout = null;
}
<div id="EditXmlImport">
    <table class="table table-bordered">
        <thead>
            <tr>
                <td>XML Id</td>
                <td>XML Stok Id</td>
                <td>XML Stok Kodu</td>
                <td>XML Barkod</td>
                <td>Alış Fiyatı</td>
                <td>Miktar</td>
                <td>Yolda</td>
                <td>Son Düzenleme</td>
                <td>Güncelle</td>
            </tr>
        </thead>
        <tbody>
            @foreach (var item in Model.StockXml)
            {
                <tr>
                    <td>
                        <input name="XmlId@(item.XmlKartId)" value="@(item.XmlId)" data-productid="@(Model.ProductId)" 
                            data-xmlid="@(item.XmlId)" data-original="@(item.XmlId)" data-field="XMLID" />
                    </td>
                    <td>
                        <input name="XmlKartId@(item.XmlKartId)" value="@(item.XmlKartId)" data-productid="@(Model.ProductId)" 
                            data-xmlid="@(item.XmlId)" data-original="@(item.XmlKartId)" data-field="XMLKARTID" />
                    </td>
                    <td>
                        <input name="XmlKartKod@(item.XmlKartId)" value="@(item.XmlKartKod)" data-productid="@(Model.ProductId)" 
                            data-xmlid="@(item.XmlId)" data-original="@(item.XmlKartKod)" data-field="XMLKARTKOD" />
                    </td>
                    <td>
                        <input name="XmlKartBarkod@(item.XmlKartId)" value="@(item.XmlKartBarkod)" data-productid="@(Model.ProductId)" 
                            data-xmlid="@(item.XmlId)" data-original="@(item.XmlKartBarkod)" data-field="XMLKARTBARKOD" />
                    </td>
                    <td>
                        <input name="Alisfiyati@(item.XmlKartId)" value="@(item.Alisfiyati)" data-productid="@(Model.ProductId)" 
                            data-xmlid="@(item.XmlId)" data-original="@(item.Alisfiyati)" data-field="ALISFIYATI" />
                    </td>
                    <td>
                        <input name="StokMiktar@(item.XmlKartId)" value="@(item.StokMiktar.ToString("###,###,##0"))" data-productid="@(Model.ProductId)" 
                            data-xmlid="@(item.XmlId)" data-original="@(item.StokMiktar.ToString("###,###,##0"))" data-field="STOKMIKTAR" />
                    </td>
                    <td>
                        <input name="StokMiktarYolda@(item.XmlKartId)" value="@(item.StokMiktarYolda.ToString("###,###,##0"))" data-productid="@(Model.ProductId)" 
                            data-xmlid="@(item.XmlId)" data-original="@(item.StokMiktarYolda.ToString("###,###,##0"))" data-field="STOKMIKTARYOLDA" />
                    </td>
                    <td>
                        <input name="DuzelTar@(item.XmlKartId)" value="@(item.DuzelTar)" data-productid="@(Model.ProductId)" 
                            data-xmlid="@(item.XmlId)" data-original="@(item.DuzelTar)" data-field="DUZELTAR" />
                    </td>
                    <td>
                        <select name="Guncelle@(item.XmlKartId)" data-productid="@(Model.ProductId)" data-xmlid="@(item.XmlId)" data-original="@(item.DuzelTar)" data-field="GUNCELLE">
                            <option value="1" @{if (item.Guncelle)
                                                {<text> selected="selected"</text>}}>Güncelle</option>
                            <option value="0" @{if (!item.Guncelle)
                                                {<text> selected="selected"</text>}}>Güncelleme</option>
                        </select>
                    </td>
                </tr>
            }
        </tbody>
    </table>
    <div class="alignright">
        <button type="button" class="btn btn-custom1" onclick="KapatVeYenile();">Kapat ve Yenile</button>
    </div>
</div>
<script>
    $('#EditXmlImport input').live('focusout', function ()
    {
        var column = $(this);
        var row = column.parent().parent();
        var dbfield = column.attr('data-field');
        var newdata = column.val();
        var productid = column.attr('data-productid');
        var xmlid = column.attr('data-xmlid');
        var original = column.attr('data-original');
        if (original != newdata)
        {
            UpdateExtraField(dbfield, newdata, productid, xmlid, original, row, column);
        }
    });
    $('#EditXmlImport select').live('change', function ()
    {
        var column = $(this);
        var row = column.parent().parent();
        var dbfield = column.attr('data-field');
        var newdata = column.val();
        var productid = column.attr('data-productid');
        var xmlid = column.attr('data-xmlid');
        var original = column.attr('data-original');
        UpdateExtraField(dbfield, newdata, productid, xmlid, original, row, column);
    });

    function UpdateExtraField(dbfield, newdata, productid, xmlid, original, row, column)
    {
        loadingMsg();
        $.post('/ProductManage/UrunTedarikGuncelle', { Field: dbfield, Value: newdata, ProductId: productid, XmlId: xmlid, Original: original }, function (data)
        {
            $('.loadingMsg').remove();
            if (data == 'success')
            {
                column.attr('data-original', newdata);
                column.attr('value', newdata);
                if (dbfield == 'XMLID')
                {
                    row.find('input').attr('data-xmlid', newdata);
                }
            }
            else
            {
                column.val(original);
                hataMesaji('Güncelleme işlemi sırasında bir hata oluştu.');
            }
        });
    }
</script>
